//
//  UserDefaults+Extension.swift
//  MCAPI
//
//  Created by MC on 2018/11/26.
//

import Foundation
import UIKit
public extension UserDefaults {
    
    /**
     * 版本信息
     */
    struct Version : UserDefaultsSettable {
        public enum defaultKeys: String {
            case version
            case build
            case registrationID
        }
    }
    struct AccountInfo : UserDefaultsSettable {
        public enum defaultKeys: String {
            case privateKey
            case sessionId
            case firstLoad
            case signDay
        }
    }
    struct ProvinceData : UserDefaultsSettable {
        public enum defaultKeys: String {
            case provAndCity
            case version
        }
    }
    struct HistorySearch:UserDefaultsSettable{
        public enum defaultKeys: String {
            case historySearch
            case historySearchLocal
            case historySearchArr
            case historySearchArrJD
            case historySearchArrPDD
        }
    }
    
    struct SplashAd: UserDefaultsSettable{
        public enum defaultKeys: String {
            case h5JumpUrl
            case iconPath
            case iosJumpUrl
            case jumpMode
            case title
            case appJumpDetailId
            case needJump
            case body
        }
    }
}

public protocol UserDefaultsSettable {
    associatedtype defaultKeys: RawRepresentable
}

extension UserDefaultsSettable where defaultKeys.RawValue==String {
    static public func set(value: Any, forKey key: defaultKeys) {
        let aKey = key.rawValue
        UserDefaults.standard.set(value, forKey: aKey)
    }
    
    
    static public func getString(forKey key: defaultKeys) -> String? {
        let aKey = key.rawValue
        let value = UserDefaults.standard.string(forKey: aKey)
        return value
    }
    
    static public func getArray(forKey key: defaultKeys) -> [Any]? {
        let aKey = key.rawValue
        
        let value = UserDefaults.standard.array(forKey: aKey)
        
        return value
    }
    
    static public func getDictionary(forKey key: defaultKeys) -> [String:Any]? {
        let aKey = key.rawValue
        
        let value = UserDefaults.standard.dictionary(forKey: aKey)
        return value
    }


}

